<template>
  <el-container>
    <!-- 1.顶部 -->
    <el-header>
      <div class="left">
        <img src="@/assets/logo.png" alt="">
        <span class="title">电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 2.主体 -->
    <el-container class="main">
      <!-- 2.1 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="collapse" @click="isCollapse = !isCollapse"> | | | </div>
<!-- background-color="#545c64" 菜单背景色 
  active-text-color="#409EFF" 点击当前菜单的颜色
 :collapse-transition="false" 关闭切换动画
 :collapse="isCollapse" 菜单是否能折叠
 unique-opened	是否只保持一个子菜单的展开
 router 开启路由默认
 :default-active='字符'默认的展开路由;
-->
        <el-menu default-active="2" class="el-menu-vertical-demo" 
          background-color="#333744" text-color="#fff"
          :collapse-transition="false"
          :collapse="isCollapse"
          unique-opened 
          router
          active-text-color="#409EFF">     
             <!-- 一级菜单  -->
          <el-submenu :index="item.id+''" v-for="(item) in menus" :key='item.id'>
            <template slot="title">
              <i :class="iconsObj[''+item.id]" class="icon-custom-style"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/'+it.path" v-for="it in item.children" :key="it.id">
              <i class="el-icon-menu"></i>
              <span slot="title">{{ it.authName }}</span>
            </el-menu-item>
          </el-submenu>
        </el-menu>

      </el-aside>
      <!-- 2.2右边主图部分 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
import {getMenus} from '@/api/home'
export default {
  name: "home-com",
  data() {
    return {
      isCollapse: false,
      iconsObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      menus:[],//储存菜单的数据
    }
  },
  created(){
    this.getMenus()
  }, 
  methods: {
    async getMenus(){
      // 1.发送请求
      let {data: {data,meta}} = await getMenus()
      // 2.如果  请求失败 则 弹出提示
      if(meta.status!==200) return this.$message.fail("请求失败!")
      // 3. 请求的数据 赋值
      this.menus = data;
    },
    // 点击退出按钮
    async logout(){
       let res = await  this.$confirm('确定退出登录吗? 亲! ', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).catch(err=>err)
          // 如果点击的是取消 则 不再往下执行
         if(res=='cancel') return ;
        // 如果点击的是确定 则 退出登录 
        // 1. 清除token
        window.localStorage.removeItem("login-token")
        // 2.跳转页面
        this.$router.push('/login')
    }
  }
}
</script>
<style lang="scss">
.main {
  // calc 计算属性 
  height: calc(100vh - 60px);
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;

  img {
    height: 60px;
    border-radius: 50%;
    vertical-align: middle;
  }

  .title {
    font-size: 24px;
    color: #fff;
  }
}

.el-aside {
  background-color: #333744;
  overflow: hidden;
}

.collapse {
  background-color: #4a5064;
  text-align: center;
  color: #fff;
  font-size: 10px;
  height: 24px;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
}
.icon-custom-style{
  font-size:16px;
  margin-right:10px;
}
.el-main{
  background-color: #eaedf1;
}
</style>
  